import { previewAttach} from '@/api/manage';
import Vue from 'vue';

export function secondsToStr(temp) {
  const years = Math.floor(temp / 31536000)
  if (years) {
    return years + ' 年' + numberEnding(years)
  }
  const days = Math.floor((temp %= 31536000) / 86400)
  if (days) {
    return days + ' 天' + numberEnding(days)
  }
  const hours = Math.floor((temp %= 86400) / 3600)
  if (hours) {
    return hours + ' 小时' + numberEnding(hours)
  }
  const minutes = Math.floor((temp %= 3600) / 60)
  if (minutes) {
    return minutes + ' 分钟' + numberEnding(minutes)
  }
  const seconds = temp % 60
  return seconds + ' 秒' + numberEnding(seconds)
  function numberEnding (number) {
    return (number > 1) ? number+'s' : 0
  }
}
export  function formatSize(size) {
  if (size < 1024) {
    return size.toFixed(0) + ' bytes'
  } else if (size < 1024 * 1024) {
    return (size / 1024.0).toFixed(0) + ' KB'
  } else if (size < 1024 * 1024 * 1024) {
    return (size / 1024.0 / 1024.0).toFixed(1) + ' MB'
  } else {
    return (size / 1024.0 / 1024.0 / 1024.0).toFixed(1) + ' GB'
  }
}
export function kebabCase(s) {
  return s.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`)
}

export const defautlCategoryMap={
 
  image: ['gif', 'jpg', 'jpeg', 'png', 'bmp', 'webp'],
  video: ['mp4', 'm3u8', 'rmvb', 'avi', 'swf', '3gp', 'mkv', 'flv'],
  audio: ['mp3', 'wav', 'wma', 'ogg', 'aac', 'flac'],
  document: ['doc', 'txt', 'docx', 'pages', 'epub', 'pdf', 'numbers', 'csv', 'xls', 'xlsx', 'keynote', 'ppt', 'pptx']

}

export  function getExtension(fileName) {
  return  !fileName?'': fileName.substr((~-fileName.lastIndexOf('.') >>> 0) + 2).toLowerCase();
 }
export function isPreview(fileName){
  let extension = getExtension(fileName)
  
  let openPdf = ['gif', 'jpg', 'jpeg', 'png', 'bmp', 'webp','doc',  'docx','ppt', 'pptx','xls', 'xlsx','pdf']
  return openPdf.includes(extension)
}




export function arrayBufferConcat() {

  
  let totalLength = 0;
  for (let arr of arguments) {
      totalLength += arr.byteLength;
  }
  let result = new Uint8Array(totalLength);
  let offset = 0;
  for (let arr of arguments) {
      result.set(arr, offset);
      offset += arr.byteLength;
  }
  
  return result;
}
//通用附件返回信息转换

export function resultChange (res) {
  let result = {}
  result.attchResult = res
  result.success = res.code == 1 ? true:false
  result.message = res.msg
  result.result = res.data
  result.code =  res.code == 1 ? 200:500
  return result
}


export function handleApprvalPreview(attach,that){
      
  attach.dbKey = !attach.dbKey? 'Trd':attach.dbKey
  if(isPreview(attach.fileName)){
    previewAttach(attach).then(result=>{
      const url=URL.createObjectURL(result)
      window.open(process.env.BASE_URL+'/pdf/web/viewer.html?file=' + url)
    }).catch(err => {
      that.$message.warning('下载文件失败');
    })
  }else{
    let href =
    that.API_BASE_URL +'/tp/attach/download?id=' +
      attach.id +
      '&dbKey='+ attach.dbKey+'&ftpKey='+attach.ftpKey+'&token=' +
      Vue.ls.get(ACCESS_TOKEN)+'&fullfilename='+attach.fileName
    window.open(process.env.VUE_APP_ONLINE_BASE_URL+'?url='+encodeURIComponent(Base64.encode(href)));
  }
}